package cn.lopr.im.api.controller;

import cn.lopr.im.api.common.CallbackMessage;
import cn.lopr.im.api.common.Constant;
import cn.lopr.im.api.exception.AuthException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author shuangyang7
 * @date 2021/5/15 17:18
 */
@RestControllerAdvice
public class BaseExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CallbackMessage<?> error(Exception e) {
        System.out.println(e.getClass());
        e.printStackTrace();
        if (e instanceof MyBatisSystemException) {
            return CallbackMessage.createByErrorCode(Constant.ResponseCode.ILLEGAL_ARGUMENT.getCode());
        } else if (e instanceof AuthException) {
            return CallbackMessage.createByError(Constant.ResponseCode.AUTH_ERROR.getCode(), e.getMessage());
        }

        return CallbackMessage.createByError(e.getMessage());
    }

}